package com.taoyibao.hongcanting.dialog;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.utils.FileUtils;
import com.taoyibao.hongcanting.widget.WheelView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * @author TangAnna
 * @description: 选择民族弹窗
 * @date :${DATA} 17:11
 */
@SuppressLint("ValidFragment")
public class DialogChooseEthnic extends BaseFragmentDialog implements View.OnClickListener {
    private WheelView mWheelView;
    /**
     * 完成按钮
     */
    private TextView mTvConfirm;
    private ArrayList<String> mData = new ArrayList<>();
    private DialogEthnicInterface mDialogEthnicInterface;

    public DialogChooseEthnic(DialogEthnicInterface dialogEthnicInterface) {
        mDialogEthnicInterface = dialogEthnicInterface;
    }

    @Override
    public int bindView() {
        return R.layout.dialog_choose_gender;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        mWheelView = view.findViewById(R.id.wv_dialogChooseGender_gender);
        mTvConfirm = view.findViewById(R.id.tv_dialogChooseGender_confirm);
    }

    @Override
    public void initData() {
        super.initData();
        String json = FileUtils.readAssetsFile("ethnic.json");
        try {
            JSONObject obj = new JSONObject(json);
            JSONArray arr = obj.getJSONArray("data");
            for (int i = 0; i < arr.length(); i++) {
                mData.add(arr.getString(i));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setData() {
        super.setData();
        mWheelView.setData(mData);
        mWheelView.setDefault(0);
    }

    @Override
    public void setListener(View view) {
        super.setListener(view);
        mTvConfirm.setOnClickListener(this);

    }

    @Override
    protected int setAnimation() {
        return 0;
    }

    @Override
    protected int setGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_dialogChooseGender_confirm://完成
                if (mDialogEthnicInterface != null && !TextUtils.isEmpty(mWheelView.getSelectedText())) {
                    mDialogEthnicInterface.onEthnicResult(mWheelView.getSelectedText());
                }
                dismiss();
                break;
        }
    }

    public interface DialogEthnicInterface {
        void onEthnicResult(String ethnic);
    }
}

